<template>
  <div class="flex">
    <ga-link @click="operationsHandleSelect('Restart')">重启</ga-link>
    <ga-link @click="operationsHandleSelect('FindPassword')">查看密码</ga-link>
    <ga-dropdown position="br" @select="operationsHandleSelect">
      <ga-link style="margin-left: 8px"> <icon-more /></ga-link>
      <template #content>
        <ga-dsubmenu value="option-1">
          <template #default>实例状态</template>
          <template #content>
            <ga-doption value="PowerOn">开机</ga-doption>
            <ga-doption value="PowerOff">关机</ga-doption>
            <ga-doption value="ForceRestart" v-if="record?.resourceType === 'smartx'">
              强制重启
            </ga-doption>
            <ga-doption value="DestroyVm">销毁</ga-doption>
          </template>
        </ga-dsubmenu>
        <ga-doption value="EditTag">编辑标签</ga-doption>
        <ga-doption value="CreateSnapshot">创建快照</ga-doption>
        <ga-dsubmenu value="option-3">
          <template #default>资源调整</template>
          <template #content>
            <ga-doption value="UpdateConfig">更改配置</ga-doption>
            <ga-doption value="DiskExpansion">磁盘扩容</ga-doption>
            <ga-doption value="AddDisk">添加磁盘</ga-doption>
          </template>
        </ga-dsubmenu>
        <ga-doption value="Migrate">迁移</ga-doption>
        <ga-doption value="NetCardConfig">网卡配置</ga-doption>
        <ga-doption value="Clone">克隆实例？</ga-doption>
        <ga-doption value="CreateImage">制作镜像</ga-doption>
        <ga-doption value="BindImage">绑定镜像</ga-doption>
        <!-- <ga-doption value="Console">控制台</ga-doption> -->
        <!-- <ga-doption value="ip">IP</ga-doption> -->
        <ga-dsubmenu value="option-4">
          <template #default>插件</template>
          <template #content>
            <ga-doption value="InstallAgent">安装蓝鲸</ga-doption>
            <ga-doption value="AgentLog">执行记录</ga-doption>
          </template>
        </ga-dsubmenu>
        <!-- <ga-dsubmenu value="option-5">
          <template #default>插件</template>
          <template #content>
            <ga-doption value="plugLog">执行记录</ga-doption>
          </template>
        </ga-dsubmenu> -->
        <ga-doption value="RegisterBastion">注册堡垒机</ga-doption>
        <!-- <ga-doption value="sync">同步</ga-doption> -->
      </template>
    </ga-dropdown>
  </div>
</template>

<script setup>
import { toRefs, computed } from "vue";
import { restartVm, sync } from "@/api/vm";
import { Message } from "@garco-design/web-vue";
import { warningModal } from "@/hooks/useWarningModal";
import { isEcsAdmin } from "@/utils/auth";
import { useRouter } from "vue-router";

const router = useRouter();
const props = defineProps({
  record: {
    type: Object,
    required: true,
  },
});
const { record } = toRefs(props);
const emits = defineEmits(["handleEditOrAdd", "fetchData"]);
const whiteOption = [
  "FindPassword",
  "DestroyVm",
  "EditTag",
  "CreateSnapshot",
  "UpdateConfig",
  "DiskExpansion",
  "AddDisk",
  "Migrate",
  "NetCardConfig",
  "CreateImage",
  "BindImage",
  "InstallAgent",
  "RegisterBastion",
];
const operationsHandleSelect = async (action) => {
  const { name, id } = record.value;
  if (action === "Restart") {
    warningModal(`确定要重启云机-[${name}]`, async () => {
      try {
        await restartVm([id]);
        Message.success("操作成功");
        emits("fetchData");
      } catch (err) {
        console.log(err);
      }
    });
  } else if (action === "PowerOn") {
    warningModal(`确定要开机云机-[${name}]`, async () => {
      try {
        await restartVm([id]);
        Message.success("操作成功");
        emits("fetchData");
      } catch (err) {
        console.log(err);
      }
    });
  } else if (action === "PowerOff") {
    warningModal(`确定要关机云机-[${name}]`, async () => {
      try {
        await restartVm([id]);
        Message.success("操作成功");
        emits("fetchData");
      } catch (err) {
        console.log(err);
      }
    });
  } else if (action === "ForceRestart") {
    warningModal(`确定要强制重启云机-[${name}]`, async () => {
      try {
        await restartVm([id]);
        Message.success("操作成功");
        emits("fetchData");
      } catch (err) {
        console.log(err);
      }
    });
  } else if (whiteOption?.includes(action)) {
    emits(`handle${action}`);
  } else if (action === "AgentLog") {
    router.push({
      path: `/bk/job-log/${record.value?.id}`,
      query: { name: record.value?.name },
    });
  }

  // else if (action === "sync") {
  //   try {
  //     const { code } = await sync(id);
  //     if (code === 20000) Message.success("操作成功，后台同步中");
  //     record.value.status = "syncing";
  //   } catch (e) {
  //     console.log(e);
  //   }
  // }
};
// deleteModalWithRefresh(
//   record.value,
//   restartVm,
//   () => emits("fetchData"),
//   `确定要关机云机-[${name}]`
// );
</script>

<style scoped lang="less"></style>
